/**
 *
 */
var PanTemp = (function () {
    // 初始化
    
    function PanTemp(options) { // 属性
        this.panid = options.panid;
        this.tempid = -1;
        this.zoom = 0.5;
        this.count = 0;
        //当前窗口有的组件个数
        this.controls = [];
        
        this.init();
        
    }
    //定时执行保存任务
  
     
    // 初始化
    PanTemp.prototype.init = function (options) {
        var that = this;
        
        //加载内容
        
        this.loadlist();
        //this.loadeditor({ "panid": this.panid });
      
    }
    
    
    PanTemp.prototype.loadlist = function (options) {
        var that = this;
      
        //
     
        $.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            url: "pans/getpanbytype",
            data: JSON.stringify({}),
            success: function (data) {
                var html = "";
                $.each(data, function (i, val) {
                    html += " <li data='{\"panid\":" + val.panid +"}' style='width:240px;'><a href='#'><img style='width:220px;height:120px' src='static/app/liujb/images/弧形柱图.png'></a><span>"+val.panname+"</span></li>";
                    
                    //初始化执行脚本
                });
                $("#ultemplist").html(html);

                $("#ultemplist li").on("click", function (e) {
                    var data = JSON.parse($(this).attr("data"));
                    that.tempid = data.panid;
                    that.loadeditor({"panid":data.panid});
                });
            }
        });

      
        function createnew(opts) {
            var pandata = {
                panname: opts.panname,
                userid: "sgx",
                pancount: 0,
                content: "[]",
                memo: "测试添加",
            };
            
            $.ajax({
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                url: "pans/insertpan",
                data: JSON.stringify(pandata),
                success: function (result) {
                    console.info(result);
                    location.href = "edit.html?tempid="+that.tempid+"&panid=" + result.insertId;
                    //window.open("edit.html ? panid =" + result.insertId);
                }
            });
        }
        
        $("#createpan").on("click", function (e) {
            //默认prompt
            layer.prompt({ title: '请输入数据大屏名称：', formType: 0 },function (val, index) {
                createnew({ "panname": val });
                layer.close(index);
            });
        });
    }
    
    PanTemp.prototype.loadeditor = function (options) {
        var that = this;
        this.panid = options.panid;
        $.each(that.controls, function (i, val) {
            $("#" + val.id + "cd").remove();
        })
        that.controls = [];
        //把之前的清除掉
        that.count = 0;
        
        var data = {
            "panid": options.panid,
        };
        //
        console.info(that.controls);
        $.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            url: "pans/getpanbyid",
            data: JSON.stringify(data),
            success: function (data) {
                that.count = data[0].pancount;
                var tmpcontous = JSON.parse(data[0].content)
                console.info(tmpcontous);
                $.each(tmpcontous, function (i, val) {
                    var cchtml = '<div data=\'' + JSON.stringify({
                        "type": val.type
                    }) + '\' id="' + val.id + 'cc" class="dragresize-content"></div>';
                    var jsobj = 'var mypan=new ' + val.type + '(); mypan.init({"id":"' + val.id + 'cc' + '"});';
                    that.createcontrol({
                        "id": val.id,
                        "type": val.type,
                        "html": cchtml,
                        "jsobj": jsobj,
                        "data": val.content,
                        "left": val.postion.left,
                        "top": val.postion.top,
                        "width": val.postion.width,
                        "height": val.postion.height,
                        "zindex": val.zindex
                    });
                    //初始化执行脚本
                });
            }
        });
    }
   
    PanTemp.prototype.createcontrol = function (options) {
        var that = this;
        
        //拖拽边框样式
        var style = 'left:' + options.left + ';top:' + options.top + ';width:' + options.width + ';height:' + options.height +
        ';background:transparent; text-align: center;';
        if (options.zindex) {
            style += 'z-index:' + options.zindex;
        }
        var _daragdiv = '<div id="' + options.id + 'cd" data=\'{"id":"' + options.id +
        'cd"}\' class="drsElement drsMoveHandle" style="' + style + '">' + options.html + '</div>';
        var dragdiv = $(_daragdiv);
        //内容对象
        var contentdiv = dragdiv.find("#" + options.id + "cc");
        
        
        $("#workcenter").append(dragdiv);
        //初始化对象内容        
        eval(options.jsobj);
        //添加到列表对象
        that.controls.push({
            "id": options.id,
            "type": options.type,
            "content": options.data,
            "zindex":0,
            "tclass": mypan
        });
        console.info(that.controls);
        
  
      
    }
    
   
    // 返回
    return PanTemp;
})();

var pantemp;
$(function () {
    var panid = getquery("panid", location.href);
    pantemp = new PanTemp({"panid":panid});
    
})